package com.lhframework.socket.controller;

import com.alibaba.fastjson.JSON;
import com.lhframework.socket.controller.support.AjaxResponse;
import com.lhframework.socket.model.RealTrack;
import com.lhframework.socket.repository.RealTrackRepository;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by lh on 2017/8/11.
 */
@RestController
@RequestMapping("/track")
public class RealTrackController {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RealTrackRepository trackRepository;

    @ApiOperation(value = "查询某个用户在某个活动的轨迹")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "activityId", value = "活动ID", required = true, paramType = "path", dataType = "long"),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "path", dataType = "long")
    })
    @RequestMapping(value = "/query/{activityId}/{userId}", method = RequestMethod.GET)
    public AjaxResponse query(@PathVariable Long activityId, @PathVariable Long userId) {
        logger.info("查询实时轨迹，【活动ID：{}，用户ID：{}】", activityId,userId);
        List<RealTrack> trackList = trackRepository.findByActivityIdAndUserId(activityId, userId);
        return AjaxResponse.ok().data(trackList);
    }

    @ApiOperation(value = "添加实时轨迹")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public AjaxResponse add(@RequestBody RealTrack realTrack) {
        logger.info("添加实时轨迹>>>>>" + JSON.toJSONString(realTrack));
        trackRepository.save(realTrack);
        return AjaxResponse.ok();
    }
}
